package com.platform.usercenter.controller;

import java.util.HashSet;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.platform.common.core.domain.base.BaseRequest;
import com.platform.common.core.utils.StringUtils;
import com.platform.common.security.utils.SecurityUtils;
import com.platform.usercenter.domain.dto.SysOrganizationDTO;
import com.platform.usercenter.utils.EmptyUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.platform.common.security.annotation.RequiresPermissions;
import com.platform.usercenter.domain.SysOrganization;
import com.platform.usercenter.service.ISysOrganizationService;
import com.platform.common.core.web.controller.BaseController;
import com.platform.common.core.web.domain.AjaxResult;
import com.platform.common.core.utils.poi.ExcelUtil;
import com.platform.common.core.web.page.TableDataInfo;

/**
 * 组织管理
 *
 * @author platform
 * @date 2024-10-24
 */
@RestController
@RequestMapping("/organization")
public class SysOrganizationController extends BaseController
{
    @Resource
    private ISysOrganizationService sysOrganizationService;

    /**
     * 查询组织列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysOrganization sysOrganization)
    {
        startPage();
        List<SysOrganization> list = sysOrganizationService.selectSysOrganizationList(sysOrganization);
        return getDataTable(list);
    }


    /**
     * 组织树结构查询
     */
    @GetMapping("/listTree")
    public AjaxResult listTree(SysOrganization sysOrganization){
        return AjaxResult.success(sysOrganizationService.listTree(sysOrganization));
    }


    /**
     * 导出组织列表
     */
    @RequiresPermissions("usercenter:organization:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysOrganization sysOrganization)
    {
        List<SysOrganization> list = sysOrganizationService.selectSysOrganizationList(sysOrganization);
        ExcelUtil<SysOrganization> util = new ExcelUtil<SysOrganization>(SysOrganization.class);
        util.exportExcel(response, list, "组织数据");
    }

    /**
     * 获取组织详细信息
     */
    @RequiresPermissions("usercenter:organization:query")
    @GetMapping(value = "/{orgId}")
    public AjaxResult getInfo(@PathVariable("orgId") Long orgId)
    {
        return success(sysOrganizationService.selectSysOrganizationInfo(orgId));
    }

    /**
     * 新增组织
     */
//    @RequiresPermissions("usercenter:organization:add")
    @PostMapping("add")
    public AjaxResult insertSysOrganization(@RequestBody SysOrganizationDTO dto)
    {
        if(StringUtils.isAnyEmpty(dto.getOrgName())){
            return AjaxResult.error("必填参数缺失！");
        }
        return toAjax(sysOrganizationService.insertSysOrganizationInfo(dto));
    }


    /**
     * 修改组织
     */
    @RequiresPermissions("usercenter:organization:edit")
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody SysOrganizationDTO sysOrganization)
    {
        return toAjax(sysOrganizationService.updateSysOrganizationInfo(sysOrganization));
    }

    /**
     * 删除组织
     */
    @RequiresPermissions("usercenter:organization:remove")
	@DeleteMapping("/{orgIds}")
    public AjaxResult remove(@PathVariable Long[] orgIds)
    {
        return toAjax(sysOrganizationService.deleteSysOrganizationByOrgIds(orgIds));
    }

    /**
     * 获取当前用户拥有权限的组织
     */
    @GetMapping("/getUerOrg")
    public AjaxResult getUerOrg(){
        Long userId = SecurityUtils.getLoginUser().getSysUser().getUserId();
        return AjaxResult.success(sysOrganizationService.listTreeByUserId(userId));

    }

    /**
     * 获取当前用户拥有权限的组织
     */
    @GetMapping("/getAirportListByOrgAndUser")
    public AjaxResult getAirportListByOrgAndUser( BaseRequest res ) throws JsonProcessingException {
        if(ObjectUtil.isEmpty(res.getOrgId())||ObjectUtil.isEmpty(res.getUserId())){
            return AjaxResult.error( "组织id和用户id不能为空" );
        }
        HashSet<String> airportList=sysOrganizationService.getAirportListByOrgAndUser(res);
        return AjaxResult.success(airportList);
    }

}
